<template>
  <div class="AddressList">
    <van-nav-bar
      title="设置"
      left-text="返回"
      right-text="更改"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      :disabled-list="disabledList"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>

<script>
import { getAddress, getdanAdd } from "../services/dress.js";
import { Toast } from "vant";
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [],
      disabledList: [],
      listdata: {},
      listdatas: [],
    };
  },
  created() {
    getAddress().then((res) => {
      console.log(res);
      // console.log(res.addresses);
      //   for (var i = 0; i < res.addresses.length; i++) {
      //     this.listdata.id = res.addresses[i]._id;
      //     this.listdata.name = res.addresses[i].receiver;
      //     this.listdata.tel = res.addresses[i].mobile;
      //     this.listdata.address =
      //       res.addresses[i].regions + res.addresses[i].address;
      //     console.log(this.listdata);
      //     console.log(this.disabledList);
      //     this.disabledList.push(this.listdata);
      //   }
      let listdatas = [];
      res.addresses.forEach((item) => {
        // console.log(item);
        let obj = {};
        obj.id = item._id;
        obj.name = item.receiver;
        obj.tel = item.mobile;
        obj.address = item.regions + item.address;
        obj.isDefault = item.isDefault;
        listdatas.push(obj);
      });
      // console.log(this.disabledList);
      this.list = listdatas;
    });
  },
  methods: {
    onClickLeft() {
      Toast("返回");
      this.$router.push({
        name: "SetUp",
      });
    },
    onClickRight() {
      Toast("按钮");
    },
    onAdd() {
      Toast("新增地址");
      this.$router.push({
        name: "Address",
      });
    },
    onEdit(item, index) {
      Toast("编辑地址:" + index);
      console.log(item);
      this.$router.push({
        name: "AddressModify",
        query: { ids: item.id },
      });
    },
  },
};
</script>

<style>
</style>